# -*- coding: utf-8 -*-
#导入 GPIO库
import RPi.GPIO as GPIO
import time
  
#设置 GPIO 模式为 BCM
GPIO.setmode(GPIO.BCM)
#定义 GPIO 引脚
GPIO_LED = 23
GPIO_SMOG = 24
GPIO_BUZZER = 22
#设置 GPIO 的工作方式 (IN / OUT)
GPIO.setup(GPIO_LED, GPIO.OUT)
GPIO.setup(GPIO_SMOG, GPIO.IN)
GPIO.setup(GPIO_BUZZER, GPIO.OUT)
def smog():
    #检测到烟雾 报警并亮灯
    if GPIO.input(GPIO_SMOG) == False:
        # 发送高电平信号到 LED,BUZZER 引脚
        GPIO.output(GPIO_LED, True)
        GPIO.output(GPIO_BUZZER, True)
        # 持续 10 us 
        time.sleep(0.2)
        GPIO.output(GPIO_LED, False)
        GPIO.output(GPIO_BUZZER, False)
        time.sleep(0.1)
    else:
        GPIO.output(GPIO_LED, False)
        GPIO.output(GPIO_BUZZER, False)
if __name__ == '__main__':
    try:
        while True:
            smog()
            #print(GPIO.input(GPIO_SMOG))
            #time.sleep(1)
            # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()
